package com.fy.login;

import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.Locale;
import java.util.Objects;

/**
 * @author zhufeifei 2022/8/8
 **/


public final class NetMetadata extends BaseMetadata implements Serializable {
    private String ip;
    private int port;
    private long loginDate;

    public NetMetadata() {
        super();
    }

    public NetMetadata(Role role, String username) {
        super(role, username);
    }

    public NetMetadata(Role role, String username, String ip, int port, long loginDate) {
        this(role, username);
        this.ip = ip;
        this.port = port;
        this.loginDate = loginDate;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public long getLoginDate() {
        return loginDate;
    }

    public void setLoginDate(long loginDate) {
        this.loginDate = loginDate;
    }

    @Override
    public String toString() {
        return "NetMetadata{" + "role='" + getRole().toString() + '\'' +
                "username='" + getUsername() + '\'' +
                ",ip='" + ip + '\'' +
                ", port=" + port +
                ", loginDate=" + loginDate +
                '}';
    }

    public String jsonStr() {
        StringBuilder sb = new StringBuilder(200);
        sb.append("{");
        sb.append("\t").append("\"role\": \"").append(getRole().toString().toLowerCase(Locale.ROOT)).append("\",").append("\n");
        sb.append("\t").append("\"username\": \"").append(getUsername()).append("\",").append("\n");
        sb.append("\t").append("\"ip\": \"").append(getIp()).append("\", ").append("\n");
        sb.append("\t").append("\"port\": ").append(getPort()).append(", ").append("\n");
        sb.append("\t").append("\"loginDate\": ").append(getLoginDate()).append(" ").append("\n");
        sb.append("}");
        return sb.toString();
    }

    public String rawJsonStr() {
        return jsonStr().replaceAll("[\t\n]", "");
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        NetMetadata metadata = (NetMetadata) o;
        return port == metadata.port && Objects.equals(ip, metadata.ip);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), ip, port);
    }

    @Override
    public void writeObject(java.io.ObjectOutputStream out) throws IOException {
        super.writeObject(out);
        out.writeUTF(this.ip);
        out.writeInt(this.port);
        out.writeLong(this.loginDate);
        out.flush();
    }

    @Override
    public void readObject(java.io.ObjectInputStream in)
            throws IOException, ClassNotFoundException {
        super.readObject(in);
        this.ip = in.readUTF();
        this.port = in.readInt();
        this.loginDate = in.readLong();
    }

    @Override
    public void readObjectNoData()
            throws ObjectStreamException {
        super.readObjectNoData();
        setPort(0);
        setLoginDate(0L);
        setIp("0.0.0.0");
    }
}
